<?php

declare(strict_types=1);

namespace app\chat\controller\admin;

use think\Request;
use think\Response;
use sheep\controller\Backend;
use app\chat\model\CustomerService;

class Index extends Backend
{

    /**
     * socket 初始化
     *
     * @return void
     */
    public function Init()
    {
        // 当前管理员
        $admin = $this->auth()->user();
        $token = $admin->getUnifiedToken('admin:' . $admin->id);    // 统一验证 token

        // 客服配置
        $chatSystem = config('chat.system');

        // 初始化 socket ssl 类型, 默认 cert
        $ssl = $chatSystem['ssl'] ?? 'none';
        $chat_domain = ($ssl == 'none' ? 'http://' : 'https://') . request()->host(true) . ($ssl == 'reverse_proxy' ? '' : (':' . $chatSystem['port'])) . '/chat';

        $data = [
            'token' => $token,
            'chat_domain' => $chat_domain,
            'default_rooms' => CustomerService::defaultRooms()
        ];
        return success('获取成功', $data);
    }
}
